package org.example.multiThreading.creationThreads;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class MyCallable {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //使用匿名内部类创建
        Callable<Integer> callable = new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                Integer i = 0;
                for (int i1 = 0; i1 < 100; i1++) {
                    i+=i1;
                }
                return i;
            }
        };
        //创建FutureTask结束Callable返回的数据
        FutureTask<Integer> futureTask = new FutureTask<>(callable);

        //创建Thread对象执行
        Thread thread = new Thread(futureTask);
        thread.start();
        //获取返回值
        System.out.println(futureTask.get());
    }
}
